home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Chans / x40088 / ut_rts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  11.4 KB  |  492 lines

  1. /* ut_rts.c - X400 rts utilities */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Chans/x40088/RCS/ut_rts.c,v 6.0 1991/12/18 20:14:27 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Chans/x40088/RCS/ut_rts.c,v 6.0 1991/12/18 20:14:27 jpo Rel $
  9.  *
  10.  * $Log: ut_rts.c,v $
  11.  * Revision 6.0  1991/12/18  20:14:27  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include <isode/rtsap.h>
  19. #include "util.h"
  20. #include "rtsparams.h"
  21. #include "chan.h"
  22. #include "Trans-types.h"
  23.  
  24. static char        *build_inkey();
  25. static char        *pretty_key = NULLCP;       /* -- for logging only -- */
  26. static void        print_remote_info();
  27. static int        remote_info_not_found ();
  28.  
  29. char            *remote_site = NULLCP;
  30. char            *undefined_bodyparts = NULLCP;
  31.  
  32.  
  33.  
  34. /* ---------------------  Begin     Routines  -------------------------------- */
  35.  
  36.  
  37.  
  38.  
  39. int rts_encode_request (ppe, request, mta, pass, isacs)
  40. PE                *ppe;
  41. struct type_Trans_Bind1988Argument **request;
  42. char                *mta, *pass;
  43. {
  44.     struct type_Trans_Bind1988Argument    *req;
  45.  
  46.     PP_TRACE (("rts_encode_request (%s %s)", mta, pass));
  47.  
  48.     req = (struct type_Trans_Bind1988Argument *)
  49.         calloc (1, sizeof (*req));
  50.     if (req == NULL)  return NOTOK;
  51.  
  52.  
  53.     if (mta == NULLCP)
  54.         req -> offset = type_MTA_MTABindArgument_no__auth;
  55.     else {
  56.         req->un.auth =
  57.             (struct member_MTA_11 *)
  58.                 calloc (1, sizeof (*req->un.auth));
  59.         req->offset = type_MTA_MTABindArgument_auth;
  60.         req -> un.auth -> initiator__name =
  61.             str2qb (mta, strlen (mta), 1);
  62.         req -> un.auth -> initiator__credentials =
  63.             (struct type_MTA_InitiatorCredentials *)
  64.                 smalloc (sizeof (struct type_MTA_InitiatorCredentials));
  65.         req -> un.auth -> initiator__credentials ->
  66.             offset = type_MTA_InitiatorCredentials_octetstring;
  67.         req -> un.auth -> initiator__credentials ->
  68.             un.octetstring = str2qb (pass, strlen (pass), 1);
  69.     }
  70.     *request = req;
  71.  
  72.     if (isacs) {
  73.         if (encode_Trans_Bind1988Result (ppe, 1, 0,
  74.                             NULLCP, req) == NOTOK) {
  75.             PP_LOG (LLOG_EXCEPTIONS,
  76.                 ("Can't encode bind request: %s", PY_pepy));
  77.             return NOTOK;
  78.         }
  79.     }
  80.     else {
  81.         if (encode_MTA_MTABindArgument (ppe, 1, 0,
  82.                         NULLCP, req) == NOTOK) {
  83.             PP_LOG (LLOG_EXCEPTIONS,
  84.                 ("Can't encode bind request: %s", PY_pepy));
  85.             return NOTOK;
  86.         }
  87.     }
  88.  
  89.     PP_TRACE (("encode Bind Argument successful!"));
  90.  
  91.     return OK;
  92. }
  93.  
  94. int rts_encode_response (ppe, result, mta, pass, isacs)
  95. PE                *ppe;
  96. struct type_Trans_Bind1988Result **result;
  97. char                *mta, *pass;
  98. {
  99.     struct type_Trans_Bind1988Result    *res;
  100.  
  101.     PP_TRACE (("rts_encode_result (%s %s)", mta, pass));
  102.  
  103.     res = (struct type_Trans_Bind1988Result *)
  104.         calloc (1, sizeof (*res));
  105.     if (res == NULL)  return NOTOK;
  106.  
  107.  
  108.     if (mta == NULLCP)
  109.         res -> offset = type_MTA_MTABindResult_no__auth;
  110.     else {
  111.         res->un.auth =
  112.             (struct member_MTA_12 *)
  113.                 calloc (1, sizeof (*res->un.auth));
  114.         res->offset = type_MTA_MTABindResult_auth;
  115.         res -> un.auth -> responder__name =
  116.             str2qb (mta, strlen (mta), 1);
  117.         res -> un.auth -> responder__credentials =
  118.             (struct type_MTA_ResponderCredentials *)
  119.                 smalloc (sizeof (struct type_MTA_ResponderCredentials));
  120.         res -> un.auth -> responder__credentials ->
  121.             offset = type_MTA_ResponderCredentials_octetstring;
  122.         res -> un.auth -> responder__credentials ->
  123.             un.octetstring = str2qb (pass, strlen (pass), 1);
  124.     }
  125.     *result = res;
  126.  
  127.     if (isacs) {
  128.         if (encode_Trans_Bind1988Result (ppe, 1, 0,
  129.                             NULLCP, res) == NOTOK) {
  130.             PP_LOG (LLOG_EXCEPTIONS,
  131.                 ("Can't encode bind request: %s", PY_pepy));
  132.             return NOTOK;
  133.         }
  134.     }
  135.     else {
  136.         if (encode_MTA_MTABindResult (ppe, 1, 0,
  137.                         NULLCP, res) == NOTOK) {
  138.             PP_LOG (LLOG_EXCEPTIONS,
  139.                 ("Can't encode bind request: %s", PY_pepy));
  140.             return NOTOK;
  141.         }
  142.     }
  143.  
  144.     PP_TRACE (("encode Bind Argument successful!"));
  145.  
  146.     return OK;
  147. }
  148.  
  149.  
  150.  
  151.  
  152. int parameter_checks (pe, mta, pass, key, checks, isacs)
  153. PE    pe;
  154. char    *mta; 
  155. char    *pass;
  156. char    *key;
  157. int    checks;
  158. int    isacs;
  159. {
  160.     struct type_Trans_Bind1988Argument *ba;
  161.     struct member_MTA_11 *rq;
  162.     char                *remotesite = NULLCP;
  163.     char                *password = NULLCP;
  164.     int                retval = NOTOK;
  165.  
  166.     PP_TRACE (("parameter_checks('%s', '%s', '%s', '%s')",
  167.         key ? key : "null", 
  168.         mta ? mta : "null",
  169.         pass ? pass : "null", 
  170.         checks ? "true" : "false"));
  171.  
  172.     if (isacs) {
  173.         if (decode_Trans_Bind1988Argument (pe, 0, NULLIP,
  174.                               NULLVP, &ba) == NOTOK) {
  175.             PP_LOG (LLOG_EXCEPTIONS,
  176.                 ("Can't decode request: %s", PY_pepy));
  177.             goto parameter_checks_free;
  178.         }
  179.     }
  180.     else {
  181.         if (decode_MTA_MTABindArgument (pe, 0, NULLIP,
  182.                         NULLVP, &ba) == NOTOK) {
  183.             PP_LOG (LLOG_EXCEPTIONS,
  184.                 ("Can't decode request: %s", PY_pepy));
  185.             goto parameter_checks_free;
  186.         }
  187.     }
  188.     print_remote_info (key, ba);
  189.  
  190.     if (checks == FALSE) {
  191.         retval = OK;
  192.         goto parameter_checks_free;
  193.     }
  194.  
  195.     if (ba->offset == type_MTA_MTABindArgument_no__auth) {
  196.         PP_NOTICE (("parameter checks are not required"));
  197.         retval = OK;
  198.         goto parameter_checks_free;
  199.     }
  200.  
  201.     rq = ba->un.auth;
  202.  
  203.     remotesite = qb2str (rq->initiator__name);
  204.  
  205.     switch (rq -> initiator__credentials -> offset) {
  206.         case type_MTA_InitiatorCredentials_octetstring:
  207.         password = qb2str (rq -> initiator__credentials ->
  208.                    un.octetstring);
  209.         break;
  210.         case type_MTA_InitiatorCredentials_ia5string:
  211.         password = qb2str (rq -> initiator__credentials ->
  212.                    un.ia5string);
  213.         break;
  214.         default:
  215.         goto parameter_checks_free;
  216.     }
  217.  
  218.     PP_TRACE (("parameter_checks MTA=%s, P=%s", remotesite, password));
  219.  
  220.     if (mta && lexequ (remotesite, mta) != 0) {
  221.         PP_LOG (LLOG_EXCEPTIONS,
  222.              ("MTAName mismatch: specified='%s' received='%s'", 
  223.             mta, remotesite));
  224.         goto parameter_checks_free;
  225.     }
  226.  
  227.  
  228.     if (lexequ (pass, "dflt") == 0) {
  229.         retval = OK;
  230.         goto parameter_checks_free;
  231.     }
  232.  
  233.  
  234.     if (pass && lexequ ((char *)password, pass) != 0) {
  235.         PP_LOG (LLOG_EXCEPTIONS, 
  236.             ("Password mismatch: specified='%s' received='%s'", 
  237.              pass, (char *)password));
  238.         goto parameter_checks_free;
  239.     }
  240.  
  241.     retval = OK;
  242.  
  243. parameter_checks_free: ;
  244.     if (ba)        free_Trans_Bind1988Argument (ba);
  245.     if (remotesite)        free (remotesite);
  246.     if (password)        free ((char *)password);
  247.  
  248.     return retval;
  249. }
  250.  
  251.  
  252.  
  253.  
  254. /* -- Checks P1 User-data: mTAName and password -- */
  255.  
  256. rts_decode_request (rts, ppe, prq, chan, isacs)
  257. struct RtSAPstart        *rts;
  258. PE                *ppe;
  259. struct type_Trans_Bind1988Result    **prq;
  260. CHAN                *chan;
  261. int    isacs;
  262. {
  263.     char        *key;
  264.     RtsParams    *rp;
  265.     int        checkit = TRUE;
  266.  
  267.     key = build_inkey (rts, isacs);
  268.  
  269.     PP_TRACE (("rts_decode_request (%s)", key));
  270.  
  271.     if ((rp = tb_rtsparams (chan -> ch_in_table, key)) == NULL) 
  272.         return (remote_info_not_found (rts, ppe, prq, key, chan, isacs));
  273.  
  274.     /* -- Check that the right info is in the tables -- */
  275.     if (rp->their_internal_ppname == NULLCP) {
  276.         PP_LOG (LLOG_EXCEPTIONS, 
  277.             ("No rname specified in the entry '%s'", key));
  278.         return RTS_VALIDATE;
  279.     }
  280.  
  281.     if (rp->their_name == NULLCP) {
  282.         PP_LOG (LLOG_EXCEPTIONS, 
  283.             ("No rmta specified in the entry '%s'", key));
  284.         return RTS_VALIDATE;
  285.     }
  286.  
  287.     if (lexequ (chan -> ch_in_info, "sloppy") == 0 
  288.         || lexequ (rp -> info_mode, "sloppy") == 0)
  289.             checkit = FALSE; 
  290.  
  291.     if (parameter_checks (rts -> rts_data, rp -> their_name,
  292.                   rp -> their_passwd, key, checkit, isacs) == NOTOK) {
  293.         PP_LOG (LLOG_EXCEPTIONS, ("Parameter check failed"));
  294.         return RTS_VALIDATE;
  295.     }
  296.  
  297.     /* -- encode new Connect Accept Data -- */
  298.     rts_encode_response (ppe, prq, rp -> our_name, rp -> our_passwd, isacs);
  299.  
  300.  
  301.     /* -- set the remote site value -- */
  302.     if (remote_site)
  303.         free (remote_site);
  304.     if (rp -> their_internal_ppname == NULLCP) {
  305.         PP_LOG (LLOG_EXCEPTIONS, ("No internal name for %s", key));
  306.         remote_site = strdup (key);
  307.     }
  308.     else remote_site = strdup (rp -> their_internal_ppname);
  309.  
  310.  
  311.      /* --- *** ---
  312.      if "undefined" set in eit - replace with rp->info_undefined
  313.      --- *** --- */
  314.  
  315.      if (undefined_bodyparts) {
  316.          free (undefined_bodyparts);
  317.          undefined_bodyparts = NULLCP;
  318.     }
  319.     if (rp -> info_undefined)
  320.         undefined_bodyparts = strdup (rp -> info_undefined);
  321.  
  322.     RPfree (rp);
  323.     return OK;
  324. }
  325.  
  326.  
  327.  
  328. /* ---------------------  Static  Routines  ------------------------------- */
  329.  
  330.  
  331.  
  332.  
  333.  
  334. static int  remote_info_not_found (rts, ppe, prq, key, chan, isacs)
  335. struct RtSAPstart        *rts;
  336. PE                *ppe;
  337. struct type_Trans_Bind1988Argument    **prq;
  338. char                *key;
  339. CHAN                *chan;
  340. int    isacs;
  341. {
  342.     RtsParams        *rp;  /* --- contains default values --- */
  343.  
  344.  
  345.     PP_TRACE (("remote_info_not_found()"));
  346.  
  347.     parameter_checks (rts -> rts_data, NULLCP, NULLCP, key, FALSE, isacs);
  348.  
  349.     if (lexequ (chan -> ch_in_info, "sloppy") != 0) {
  350.         PP_LOG (LLOG_EXCEPTIONS, 
  351.             ("No information found in the X.400 incoming tables"));
  352.         return RTS_VALIDATE;
  353.     }
  354.  
  355.     if ((rp = tb_rtsparams (chan -> ch_in_table, "default")) == NULL) {
  356.         PP_LOG (LLOG_EXCEPTIONS, 
  357.             ("No 'default' found in the X.400 incoming tables"));
  358.         return RTS_VALIDATE;
  359.     }
  360.  
  361.  
  362.     /* -- encode new Connect Accept Data -- */
  363.     rts_encode_request (ppe, prq, rp -> our_name, rp -> our_passwd, isacs);
  364.     RPfree (rp);
  365.  
  366.     if (remote_site)  free (remote_site);
  367.     remote_site = strdup (key);
  368.  
  369.     PP_LOG (LLOG_EXCEPTIONS, 
  370.         ("No information found in the X.400 incoming tables - BUT have accepted the connection"));
  371.  
  372.     return OK;
  373. }
  374.  
  375.  
  376.  
  377. static void print_remote_info (key, ba)
  378. char                *key;
  379. struct type_MTA_MTABindArgument *ba;
  380. {
  381.     static char        buffer[BUFSIZ];
  382.     char            *cp;
  383.  
  384.     (void) sprintf (buffer, "key='%s' / '%s'",
  385.             key, pretty_key);
  386.  
  387.     if (ba->offset == type_MTA_MTABindArgument_no__auth) {
  388.         PP_NOTICE (("%s (no more connection data info)",
  389.                 buffer));
  390.         return;
  391.     }
  392.  
  393.     (void) strcat (buffer, " mta='");
  394.  
  395.     if ((cp = qb2str (ba->un.auth->initiator__name)) != NULLCP) {
  396.         (void) strcat (buffer, cp);
  397.         (void) strcat (buffer, "'");
  398.         free (cp);
  399.     }
  400.     (void) strcat (buffer, " passwd='");
  401.     switch (ba -> un.auth -> initiator__credentials -> offset) {
  402.         case type_MTA_InitiatorCredentials_ia5string:
  403.         cp = qb2str (ba -> un.auth
  404.                  -> initiator__credentials -> un.ia5string);
  405.         (void) strcat (buffer, cp);
  406.         free (cp);
  407.         (void) strcat (buffer, "'(IA5)");
  408.         break;
  409.         case type_MTA_InitiatorCredentials_octetstring:
  410.         cp = qb2str (ba->un.auth -> initiator__credentials
  411.                  -> un.octetstring);
  412.         (void) strcat (buffer, cp);
  413.         free (cp);
  414.         (void) strcat (buffer, "'(OCTS)");
  415.         break;
  416.         default:
  417.         (void) strcat (buffer, "<secure-credentials>'");
  418.         break;
  419.     }
  420.  
  421.     PP_NOTICE (("Connection from %s", buffer));
  422.     return;
  423. }
  424.  
  425.  
  426.  
  427. static char *build_inkey (rts, isacs)
  428. struct RtSAPstart        *rts;
  429. {
  430.     struct PSAPaddr        pas;
  431.     struct PSAPaddr        *ps;
  432.     struct NSAPaddr        *na;
  433.     int            nna;
  434.     char            *cp;
  435.  
  436.     PP_TRACE (("build_inkey()"));
  437.  
  438.     if (isacs)
  439.         ps = &rts -> rts_start.acs_start.ps_calling;
  440.     else {
  441.         bzero ((char *)&pas, sizeof pas);
  442.         pas.pa_addr = rts -> rts_initiator.rta_addr;
  443.         ps = &pas;
  444.     }
  445.         for (na = ps -> pa_addr.sa_addr.ta_addrs,
  446.                 nna = ps -> pa_addr.sa_addr.ta_naddr;
  447.                 nna > 0; nna --, na ++) {
  448.                 if (na -> na_type == NA_TCP) {
  449.                         PP_TRACE (("Address is TCP, port set to 0 from %d",
  450.                                 na -> na_port));
  451.                         na -> na_port = 0;
  452.             /* bug in isode 6.6 */
  453.                         if ((cp = index(na -> na_domain, '+')) != NULL)
  454.                                 *cp = '\0';
  455.                 }
  456.         }
  457.  
  458.     if (pretty_key)
  459.         free (pretty_key);
  460.     pretty_key = strdup(paddr2str (ps, NULLNA));
  461.  
  462.     return _paddr2str (ps, NULLNA, -1);
  463. }
  464.  
  465.  
  466. dump_pdu (qb, str)
  467. struct qbuf *qb;
  468. char *str;
  469. {
  470.     struct qbuf *q;
  471.     char    buf[512];
  472.     char    *cp;
  473.     int    len, n;
  474.  
  475.     if ((pp_log_norm -> ll_events & LLOG_PDUS) == 0)
  476.         return;
  477.  
  478.     PP_LOG (LLOG_PDUS, ("PDU dump: %s", str));
  479.     for (q = qb -> qb_forw; q != qb; q = q -> qb_forw) {
  480.         for (cp = q -> qb_data, len = q -> qb_len; 
  481.              len > 0;) {
  482.             n = min (sizeof (buf) / 2 - 1, len);
  483.             buf[explode (buf, (u_char *)cp, n)] = 0;
  484.             ll_printf (pp_log_norm, "%s", buf);
  485.  
  486.             len -= n;
  487.             cp += n;
  488.         }
  489.     }
  490.     ll_printf (pp_log_norm, "\n-----------\n");
  491. }
  492.